package com.wechat.wechat.controller;

import com.wechat.wechat.entity.ProductCategory;
import com.wechat.wechat.exception.SellException;
import com.wechat.wechat.form.CategoryForm;
import com.wechat.wechat.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author NN
 * @2020/2/26 20:18
 */
@Controller
@RequestMapping("/seller/category")
public class SellerCategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 获取类目列表 http://127.0.0.1:8080/sell/seller/category/list
     */
    @GetMapping("/list")
    public ModelAndView list(Map<String,Object> map )
    {
        List<ProductCategory> productCategoryList=categoryService.findAll();
        map.put("productCategoryList",productCategoryList);//将数据传给模板文件
        return new ModelAndView("category/list",map);
    }
    /**
     * 根据类目id查询类目 http://127.0.0.1:8080/sell/seller/category/index
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId",required = false) Integer categoryId,
                              Map<String,Object> map)
    {
        if(categoryId!=null && categoryId>0)
        {
            ProductCategory productCategory=categoryService.findOne(categoryId);
            map.put("productCategory",productCategory);
        }
        return new ModelAndView("category/index",map);
    }
    /**
     * 新增、修改类目 http://127.0.0.1:8080/sell/seller/category/save
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid CategoryForm form, BindingResult bindingResult,
                             Map<String,Object> map)
    {
        if(bindingResult.hasErrors())
        {
            map.put("msg",bindingResult.getFieldError().getDefaultMessage()); //返回错误信息
            map.put("url","/sell/seller/category/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        ProductCategory productCategory = new ProductCategory();
        try{
            if(!StringUtils.isEmpty(form.getCategoryId()))
            {
                productCategory=categoryService.findOne(form.getCategoryId());
            }
            BeanUtils.copyProperties(form,productCategory);
            categoryService.save(productCategory);

        }catch(SellException e)
        {
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/category/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        map.put("url","/sell/seller/category/list");
        return new ModelAndView("/common/success",map);
    }
}
